﻿using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wms_wpf.Apiservice;
using wms_wpf.Dto;
using wms_wpf.InPut;

namespace wms_wpf.ViewModels
{
   public class TaskOrderViewModel: BindableBase, INavigationAware
    {
        string Machine = Environment.GetEnvironmentVariable("Machine", EnvironmentVariableTarget.Machine);
        public TaskOrderViewModel()
        { 
            InPut_ViewService=new InPut_ViewService4();
            InPut_ViewService5 = new InPut_ViewService5();
            viewService=new ViewService();
            essService=new EssService();
            baseService=new BaseService();
            customService=new CustomService();
            Inlethousecodes = new List<string>();
            InlethousecodeOuts = new List<string>();
            InPut_BaseService12 =new InPut_BaseService12();
            InPut_BaseService13=new InPut_BaseService13();
            InPut_ViewService.PageIndex = 1;
            InPut_ViewService.PageSize = 20;
            InPut_ViewService5.PageIndex = 1;
            InPut_ViewService5.PageSize = 20;
            InPut_ViewService.State = "未下发";
        
            IndexCommand = new DelegateCommand<string>(Index);
            View_TaskOrderDtos = new List<View_TaskOrderDto>();
            CusetomOrderDtos = new List<CusetomOrderDto>();
            GetList_View_TaskOrderCommand = new DelegateCommand(GetList_View_TaskOrder);
            TaskCreateCommand = new DelegateCommand<IList<object>>(TaskCreate);
            CreateTaskOrderCommand = new DelegateCommand(CreateTaskOrder);
            GetList_View_BoxCommand = new DelegateCommand(GetList_View_Box);
            CreateTaskOrderOutCommand=new DelegateCommand<IList<object>>(CreateTaskOrderOut);
            DeleteTaskOrderCommand = new DelegateCommand<IList<object>>(DeleteTaskOrder);
            TaskCreate_OrderCommand = new DelegateCommand<IList<object>>(TaskCreate_Order);
   
            GetList_View_TaskOrder();
            GetList_InlethouesCode();
            GetList_InlethouesCodeOut();
            GetList_View_Box();
            if (Machine == "2")
            {
                ComboxMachine = 1;
            }
            else
            {
                ComboxMachine = 0;
            }

        }
        private readonly IViewService viewService;
        private readonly IBaseService baseService;
        private readonly ICustomService customService;
        private List<string> inlethousecodes;
        public List<string> Inlethousecodes
        {
            get { return inlethousecodes; }
            set { inlethousecodes = value; RaisePropertyChanged(); }
        }
        private List<string> inlethousecodeOuts;
        public List<string> InlethousecodeOuts
        {
            get { return inlethousecodeOuts; }
            set { inlethousecodeOuts = value; RaisePropertyChanged(); }
        }
        public DelegateCommand GetList_View_TaskOrderCommand {  get; set; }
        public DelegateCommand CreateTaskOrderCommand { get; set; }
        public DelegateCommand<IList<object>> TaskCreateCommand { get; set; }
        public DelegateCommand<string> IndexCommand { get; set; }
        public DelegateCommand GetList_View_BoxCommand { get; set; }
        public DelegateCommand GetList_View_TaskOrder_OrderNoCommand { get; set; }
        public DelegateCommand<IList<object>> DeleteTaskOrderCommand { get; set; }
        public DelegateCommand<IList<object>> CreateTaskOrderOutCommand {  get; set; }
        public DelegateCommand<IList<object>> TaskCreate_OrderCommand { get; set; }
        


        private InPut_BaseService12 inPut_BaseService12;
        public InPut_BaseService12  InPut_BaseService12
        {
            get { return inPut_BaseService12; }
            set { inPut_BaseService12 = value;RaisePropertyChanged();}
        }
        private InPut_BaseService13 inPut_BaseService13;
        public InPut_BaseService13 InPut_BaseService13
        {
            get { return inPut_BaseService13; }
            set { inPut_BaseService13 = value; RaisePropertyChanged(); }
        }

        private InPut_ViewService4 inPut_ViewService4;
        public InPut_ViewService4 InPut_ViewService
        {
            get { return inPut_ViewService4; }
            set { inPut_ViewService4 = value;RaisePropertyChanged();}
        }
        private InPut_ViewService5 inPut_ViewService5;
        public InPut_ViewService5 InPut_ViewService5
        {
            get { return inPut_ViewService5; }
            set { inPut_ViewService5 = value; RaisePropertyChanged(); }
        }

        public IEssService essService { get; set; }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }

        private long count2;
        public long Count2
        {
            get { return count2; }
            set { count2 = value; RaisePropertyChanged(); }
        }
        private long pagecount2;
        public long PageCount2
        {
            get { return pagecount2; }
            set { pagecount2 = value; RaisePropertyChanged(); }
        }
        private int comboxMachine;
        public int ComboxMachine
        {
            get { return comboxMachine; }
            set { comboxMachine = value; RaisePropertyChanged(); }
        }



        private int selectIndex;
        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }
        private List<View_TaskOrderDto> view_TaskOrderDtos;
        public  List<View_TaskOrderDto> View_TaskOrderDtos
        {
            get { return view_TaskOrderDtos; }
            set { view_TaskOrderDtos = value; RaisePropertyChanged(); }
        }
        private List<View_BoxDto> view_BoxDtos;
        public List<View_BoxDto> View_BoxDtos
        {
            get { return view_BoxDtos; }
            set { view_BoxDtos = value; RaisePropertyChanged(); }
        }
        private List<CusetomOrderDto> cusetomOrderDtos;
        public List<CusetomOrderDto> CusetomOrderDtos
        {
            get { return cusetomOrderDtos; }
            set { cusetomOrderDtos = value; RaisePropertyChanged(); }
        }
        private async void DeleteTaskOrder(IList<object> obj)
        {
            try
            {
                List<string> taskorderCodes = new List<string>();
                foreach (var item in obj)
                {
                    if (item is View_TaskOrderDto dto)
                    {
                        if (dto.State == "已下发")
                        {
                            Growl.Info("已下发任务不可删除", "msg");
                            return;
                        }
                        taskorderCodes.Add(dto.TaskOrderCode);
                    }

                }
                await baseService.DeleteTaskOrder(taskorderCodes);
                GetList_View_TaskOrder();
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-DeleteTaskOrder", ex);
            }
        
        }
        private async void CreateTaskOrderOut(IList<object> obj)
        {
            try
            {
                foreach (var item in obj)
                {
                    if (item is View_BoxDto dto)
                    {
                        InPut_BaseService13.BoxCodes.Add(dto.BoxCode);
                    }

                }

                await baseService.CreateTaskOrderOut(InPut_BaseService13);
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-CreateTaskOrderOut", ex);
            }
     

        }
        private async void GetList_View_Box()
        {
            try
            {
                Tuple<List<View_BoxDto>, long?, long?> tuple = await viewService.GetList_View_Box(InPut_ViewService5);
                if (tuple == null)
                {
                    return;
                }
                View_BoxDtos = tuple.Item1;
                Count2 = (long)tuple.Item2;
                PageCount2 = (long)tuple.Item3;
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetList_View_Box", ex);
            }
           
        }   
        private async void TaskCreate(IList<object> obj)
        {
            try
            {
                foreach (var item in obj)
                {
                    if (item is View_TaskOrderDto dto)
                    {

                        if (dto.State == "已下发") continue;
                        if (dto.Type == "出库")
                        {
                            Ess_Input ess_Input = new Ess_Input();
                            ess_Input.taskType = "carry";
                            ess_Input.taskGroupCode = "taskGroupCode-001";
                            ess_Input.groupPriority = 0;
                            List<Tasks> data = new List<Tasks>();
                            Tasks ta = new Tasks();
                            ta.taskCode = dto.TaskOrderCode;
                            ta.taskPriority = 0;
                            data.Add(ta);
                            ess_Input.tasks = data;
                            TaskDescribe td = new TaskDescribe();
                            td.containerCode = dto.BoxCode;
                            td.containerType = "CT_KUBOT_STANDARD";
                            td.storageTag = "";
                            td.fromLocationCode = dto.WarehouseCode;
                            td.toLocationCode = dto.InlethouseCode;
                            td.toStationCode = "haiport_station_1";
                            td.isPutaway = false;
                            ta.taskDescribe = td;
                            string res = await essService.CreateTask(ess_Input);
                            if (res.Contains("OK"))
                            {

                                InPut_BaseService11 inPut = new InPut_BaseService11();
                                inPut.TaskOrderCode = dto.TaskOrderCode;
                                inPut.Type = "已下发";
                                await baseService.UpdateTaskOrderState(inPut);
                                GetList_View_Box();
                                await baseService.CreateLog("下架", dto.BoxCode + "执行了下架任务");
                            }
                        }
                        if (dto.Type == "入库")
                        {
                            if (obj.Count > 1)
                            {
                                Growl.Info("入库任务只允许单个下发", "msg");
                                return;
                            }
                            string boxcode = await essService.queryNodeReader();
                            if (string.IsNullOrEmpty(boxcode))
                            {
                                Growl.Info("拣选位无料盒,无法上架", "msg"); return;
                            }
                            if (boxcode != dto.BoxCode)
                            {
                                Growl.Info("料盒码不一致,无法上架", "msg"); return;
                            }

                            Ess_Input ess_Input = new Ess_Input();
                            ess_Input.taskType = "putaway";
                            ess_Input.taskGroupCode = "taskGroupCode-001";
                            ess_Input.groupPriority = 0;
                            List<Tasks> data = new List<Tasks>();
                            Tasks ta = new Tasks();
                            ta.taskCode = dto.TaskOrderCode;
                            ta.taskPriority = 0;
                            data.Add(ta);
                            ess_Input.tasks = data;
                            TaskDescribe td = new TaskDescribe();
                            td.containerCode = dto.BoxCode;
                            td.containerType = "CT_KUBOT_STANDARD";
                            td.storageTag = "";
                            td.fromLocationCode = dto.InlethouseCode;
                            td.toLocationCode = dto.WarehouseCode;
                            td.toStationCode = "haiport_station_1";
                            td.isPutaway = false;
                            ta.taskDescribe = td;
                            string res = await essService.CreateTask(ess_Input);
                            if (res.Contains("OK"))
                            {

                                await essService.moveContainer(dto.BoxCode);
                                InPut_BaseService11 inPut = new InPut_BaseService11();
                                inPut.TaskOrderCode = dto.TaskOrderCode;
                                inPut.Type = "已下发";
                                await baseService.UpdateTaskOrderState(inPut);
                                await baseService.CreateLog("上架", dto.BoxCode + "执行了上架任务");

                            }

                        }

                    }
                }
                GetList_View_TaskOrder();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-TaskCreate", ex);
            }
        

        }
        private async void TaskCreate_Order(IList<object> obj)//工单下架
        {

            try
            {
                InPut_ViewService7 InPut_ViewService7 = new InPut_ViewService7();
                foreach (var item in obj)
                {
                    if (item is View_TaskOrder_OrderNoDto dto)
                    {
                        InPut_ViewService7.OrderNos.Add(dto.OrderNo);
                    }
                }
                List<View_TaskOrderDto> view_TaskOrder_OrderDtos = await viewService.QuaneryList_View_TaskOrder(InPut_ViewService7);
                foreach (var dto in view_TaskOrder_OrderDtos)
                {
                    if (dto.Type == "出库")
                    {
                        Ess_Input ess_Input = new Ess_Input();
                        ess_Input.taskType = "carry";
                        ess_Input.taskGroupCode = "taskGroupCode-001";
                        ess_Input.groupPriority = 0;
                        List<Tasks> data = new List<Tasks>();
                        Tasks ta = new Tasks();
                        ta.taskCode = dto.TaskOrderCode;
                        ta.taskPriority = 0;
                        data.Add(ta);
                        ess_Input.tasks = data;
                        TaskDescribe td = new TaskDescribe();
                        td.containerCode = dto.BoxCode;
                        td.containerType = "CT_KUBOT_STANDARD";
                        td.storageTag = "";
                        td.fromLocationCode = dto.WarehouseCode;
                        td.toLocationCode = dto.InlethouseCode;
                        td.toStationCode = "haiport_station_1";
                        td.isPutaway = false;
                        ta.taskDescribe = td;
                        string res = await essService.CreateTask(ess_Input);
                        if (res.Contains("OK"))
                        {

                            InPut_BaseService11 inPut = new InPut_BaseService11();
                            inPut.TaskOrderCode = dto.TaskOrderCode;
                            inPut.Type = "已下发";
                            await baseService.UpdateTaskOrderState(inPut);
                            GetList_View_Box();
                            await baseService.CreateLog("下架", dto.BoxCode + "执行了下架任务");
                        }
                    }
                }
                GetList_View_TaskOrder();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-TaskCreate_Order", ex);
            }
        
        }
        private async void GetList_InlethouesCode()
        {
            Inlethousecodes = await baseService.GetList_InlethouesCode("入口");
        }
        private async void GetList_InlethouesCodeOut()
        {
            InlethousecodeOuts= await baseService.GetList_InlethouesCode("出口");
        }
        private async void CreateTaskOrder()
        {
            try
            {
                await baseService.CreateTaskOrder(InPut_BaseService12);
                GetList_View_TaskOrder();
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-CreateTaskOrder", ex);
            }
      
        }
        private async void GetList_View_TaskOrder()
        {
            try
            {
                Tuple<List<View_TaskOrderDto>, long?, long?> tuple = await viewService.GetList_View_TaskOrder(InPut_ViewService);
                if (tuple == null)
                {
                    return;
                }
                View_TaskOrderDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-GetList_View_TaskOrder", ex);
            }

        }
  


        private void Index(string obj)
        {
            switch (obj)
            {
                case "TwoPage": SelectIndex = 1; GetList_View_Box(); break;
                case "OnePage": SelectIndex = 0; GetList_View_TaskOrder(); break;

            }
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
           
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
     
        }
    }
}
